package com.hearwego;

import java.io.File;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class TabConfiguracionFragment extends BaseFragment {
    private static final int CAMERA_PIC_REQUEST = 1337;  

    void openCamera(){
    	 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
    	// create a File object for the parent directory
    	 File wallpaperDirectory = new File(Environment.getExternalStorageDirectory()+"/HearWeGo/");
    	 // have the object build the directory structure, if needed.
    	 wallpaperDirectory.mkdirs();
    	 
    	 
         File file = new File(wallpaperDirectory, "foto"+System.currentTimeMillis()+".jpg");
         Uri outputFileUri = Uri.fromFile(file);
         cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
         Log.i("URI PHOTO", outputFileUri.toString());
         startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);  
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab_configuracion, container, false);;     //  =   inflater.inflate(R.layout.tab_configuracion, container, false);
        Button camera = (Button) view.findViewById(R.id.bt_camara);
        camera.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			       openCamera();
			}
		});
        

        return view;
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
      	Log.i("onActivityResult", "onActivityResult");

   	 if(resultCode==mActivity.RESULT_CANCELED){
      Toast.makeText(mActivity.getApplicationContext(), "No se ha tomado la foto", Toast.LENGTH_LONG).show();

      }
      else{
      	Log.i("else", "else");
      	
      	final Dialog dialog = new Dialog(mActivity);
      	dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_foto);
		Button cancelar = (Button) dialog.findViewById(R.id.bt_cancelar);
		Button aceptar = (Button) dialog.findViewById(R.id.bt_aceptar);
		final EditText et = (EditText) dialog.findViewById(R.id.et_comentario);
		
		cancelar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				openCamera();
			}
		});
		
		aceptar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(et.getText().toString().equals("")){
					Toast.makeText(mActivity.getApplicationContext(), "Comentario vacío", Toast.LENGTH_SHORT).show();
				}else{
					dialog.dismiss();
				}
			}
		});
		
		
		
		
		
		dialog.show();
      	
      	
//		AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
//		builder.setMessage("Put your question here?")
//		       .setCancelable(false)
//		       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
//		           public void onClick(DialogInterface dialog, int id) {
//		        	   openCamera();
//		           }
//		       })
//		       .setNegativeButton("No", new DialogInterface.OnClickListener() {
//		           public void onClick(DialogInterface dialog, int id) {
//		        	   // put your code here 
//		        	   dialog.cancel();
//		           }
//		       });
//		AlertDialog alertDialog = builder.create();
//		alertDialog.show();
//          if (requestCode == CAMERA_PIC_REQUEST) {
//         	 Log.i("Photo", "taken");
//         	 
//          }
     }
    
    }
    
    
    
}